package com.whfc.mach.redis.impl;

import com.whfc.mach.redis.CloudmRedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2022/3/9 14:25
 */
@Component
public class CloudmRedisDaoImpl implements CloudmRedisDao {

    private static final String CLOUDM_DEVICE = "cloudm_device";

    private static final String CLOUDM_DEVICE_DATA = "cloudm_device::{0}";

    private static final String LOC_TIME = "loc_time";

    private static final String OIL_LEVEL_TIME = "oil_level_time";

    private static final String OIL_ADD_TIME = "oil_add_time";

    private StringRedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void setDeviceId(String sn, String deviceId) {
        redisTemplate.opsForHash().put(CLOUDM_DEVICE, sn, deviceId);
    }

    @Override
    public String getDeviceId(String sn) {
        return (String) redisTemplate.opsForHash().get(CLOUDM_DEVICE, sn);
    }

    @Override
    public void setDeviceId(Map<String, String> map) {
        redisTemplate.opsForHash().putAll(CLOUDM_DEVICE, map);
    }

    @Override
    public void cleanDeviceIds() {
        redisTemplate.delete(CLOUDM_DEVICE);
    }

    @Override
    public void setDeviceLocTime(String deviceId, Date time) {
        String key = MessageFormat.format(CLOUDM_DEVICE_DATA, deviceId);
        if (time != null) {
            redisTemplate.opsForHash().put(key, LOC_TIME, String.valueOf(time.getTime()));
        }
    }

    @Override
    public Date getDeviceLocTime(String deviceId) {
        String key = MessageFormat.format(CLOUDM_DEVICE_DATA, deviceId);
        Object val = redisTemplate.opsForHash().get(key, LOC_TIME);
        if (val != null) {
            return new Date(Long.parseLong(String.valueOf(val)));
        }
        return null;
    }

    @Override
    public void setDeviceOilLevelTime(String deviceId, Date time) {
        String key = MessageFormat.format(CLOUDM_DEVICE_DATA, deviceId);
        if (time != null) {
            redisTemplate.opsForHash().put(key, OIL_LEVEL_TIME, String.valueOf(time.getTime()));
        }
    }

    @Override
    public Date getDeviceOilLevelTime(String deviceId) {
        String key = MessageFormat.format(CLOUDM_DEVICE_DATA, deviceId);
        Object val = redisTemplate.opsForHash().get(key, OIL_LEVEL_TIME);
        if (val != null) {
            return new Date(Long.parseLong(String.valueOf(val)));
        }
        return null;
    }

    @Override
    public void setDeviceOilAddTime(String deviceId, Date time) {
        String key = MessageFormat.format(CLOUDM_DEVICE_DATA, deviceId);
        if (time != null) {
            redisTemplate.opsForHash().put(key, OIL_ADD_TIME, String.valueOf(time.getTime()));
        }
    }

    @Override
    public Date getDeviceOilAddTime(String deviceId) {
        String key = MessageFormat.format(CLOUDM_DEVICE_DATA, deviceId);
        Object val = redisTemplate.opsForHash().get(key, OIL_ADD_TIME);
        if (val != null) {
            return new Date(Long.parseLong(String.valueOf(val)));
        }
        return null;
    }
}
